
package me.zhengjie.modules.workOrder.service.impl;

import me.zhengjie.modules.workOrder.domain.WorkOrderHead;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.workOrder.repository.WorkOrderHeadRepository;
import me.zhengjie.modules.workOrder.service.WorkOrderHeadService;
import me.zhengjie.modules.workOrder.service.dto.WorkOrderHeadDto;
import me.zhengjie.modules.workOrder.service.dto.WorkOrderHeadQueryCriteria;
import me.zhengjie.modules.workOrder.service.mapstruct.WorkOrderHeadMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author 李奎
* @date 2023-06-29
**/
@Service
@RequiredArgsConstructor
public class WorkOrderHeadServiceImpl implements WorkOrderHeadService {

    private final WorkOrderHeadRepository workOrderHeadRepository;
    private final WorkOrderHeadMapper workOrderHeadMapper;

    @Override
    public Map<String,Object> queryAll(WorkOrderHeadQueryCriteria criteria, Pageable pageable){
        Page<WorkOrderHead> page = workOrderHeadRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(workOrderHeadMapper::toDto));
    }

    @Override
    public List<WorkOrderHeadDto> queryAll(WorkOrderHeadQueryCriteria criteria){
        return workOrderHeadMapper.toDto(workOrderHeadRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public WorkOrderHeadDto findById(String workId) {
        WorkOrderHead workOrderHead = workOrderHeadRepository.findById(workId).orElseGet(WorkOrderHead::new);
        ValidationUtil.isNull(workOrderHead.getWorkId(),"WorkOrderHead","workId",workId);
        return workOrderHeadMapper.toDto(workOrderHead);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkOrderHeadDto create(WorkOrderHead resources) {
        resources.setCreateBy(SecurityUtils.getCurrentUsername());
        return workOrderHeadMapper.toDto(workOrderHeadRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WorkOrderHead resources) {
        WorkOrderHead workOrderHead = workOrderHeadRepository.findById(resources.getWorkId()).orElseGet(WorkOrderHead::new);
        ValidationUtil.isNull( workOrderHead.getWorkId(),"WorkOrderHead","id",resources.getWorkId());
        workOrderHead.copy(resources);
        workOrderHead.setUpdateBy(SecurityUtils.getCurrentUsername());
        workOrderHeadRepository.save(workOrderHead);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String workId : ids) {
            WorkOrderHead workOrderHead = workOrderHeadRepository.findById(workId).orElseGet(WorkOrderHead::new);
            workOrderHead.setIsDelete("0");
            workOrderHeadRepository.save(workOrderHead);
        }
    }

    @Override
    public void download(List<WorkOrderHeadDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WorkOrderHeadDto workOrderHead : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("工单名称", workOrderHead.getWorkName());
            map.put("工单描述", workOrderHead.getWorkDescrible());
            map.put("工单编号", workOrderHead.getWorkCode());
            map.put("所属项目id", workOrderHead.getProjectId());
            map.put("所属项目名称", workOrderHead.getProjectName());
            map.put("排序", workOrderHead.getOrderNumber());
            map.put("备注", workOrderHead.getRemark());
            map.put("删除标识", workOrderHead.getIsDelete());
            map.put("创建者", workOrderHead.getCreateBy());
            map.put("更新者", workOrderHead.getUpdateBy());
            map.put("创建日期", workOrderHead.getCreateTime());
            map.put("更新时间", workOrderHead.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}